package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-29
 * Time: 18:50
 */
public class ThreadDemon38 {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum = 0;
                for (int i = 0; i <= 5000; i++) {
                    sum += i;
                }
                return sum;
            }
        };
        //Thread t1 = new Thread(callable);//Thread中没有提供构造函数来传入callable
        //引入FutureTask类，未来要完成的任务（任务还未执行）
        // 相当于在Callable中确定执行的任务
        //在FutureTask装置中完成任务加载——卢本伟准备就绪~~~
        //最后引入线程——卢本伟启动！！
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread t1 = new Thread(futureTask);
        t1.start();
        t1.join();
        System.out.println(futureTask.get());//装置获得一下结果
    }
}
